(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open VMTypes
  
let tracing_bytecode = VMEvaluate.tracing_bytecode
  
(* scopes *)
type scope = Scope.scope

let make_scope = VMPrimitives.initial_scope
let bind_primitive = VMPrimitives.bind_primitive
let bind_bin_op_l = VMPrimitives.bind_bin_op_l
let bind_bin_op_n = VMPrimitives.bind_bin_op_n
let bind_bin_op_r = VMPrimitives.bind_bin_op_r
let bind_pre_op = VMPrimitives.bind_pre_op
let bind_post_op = VMPrimitives.bind_post_op

(* symbols *)
let string_to_symbol = SymbolTable.string_to_symbol
let symbol_to_string = SymbolTable.symbol_to_string
  
(* evaluation *)
let uc_string_to_char_list = VMPrimitives.uc_string_to_char_list
let uc_list_to_char_list = VMPrimitives.uc_list_to_char_list
let ascii_to_char_list = VMPrimitives.ascii_to_char_list
  
let execute_declarations scope decls =
  let code = Compile.compile_declarations scope decls
  in ignore (VMEvaluate.execute code [ ref Unbound ])
  
let execute = VMEvaluate.execute
let evaluate_lin_form = VMEvaluate.evaluate_lin_form
let evaluate_opaque = VMEvaluate.evaluate_opaque
let unify = VMEvaluate.unify
  
let set_unknown x v = VMEvaluate.forced_unify x (ref v)
  
let evaluate_expression scope stream =
  let code = Compile.compile_expression scope stream in
  let x = VMEvaluate.execute code [] in !x
  
let evaluate_function f args =
  VMEvaluate.execute [| BGlobal f; BApply (List.length args) |] args
  
let evaluate_monad_expr scope stream init =
  let code = Compile.compile_expression scope stream in
  let f = VMEvaluate.execute code [] in
  let x = evaluate_function f [ ref init ] in !x
  
let decode_string = VMPrimitives.evaluate_char_list
let decode_list = VMEvaluate.evaluate_list
let decode_num = VMEvaluate.evaluate_num
  
let evaluate_string_expr name scope stream =
  decode_string name (ref (evaluate_expression scope stream))
  
let lookup_symbol scope sym =
  let x = Scope.lookup_global scope (string_to_symbol sym) in !x

